package server.config;

import java.awt.Font;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.*;

import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;

import server.models.UserStatus;
import common.message.serverSend.ServerOffline;

/**
 * 
 * @author solomon
 *
 */
public class Public {
	
	public static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");

	public static boolean stop = false;// 控制线程的终止

	public static boolean bind = false;// 绑定标志

	public static ServerSocket serverSocket = null;

	public static UserStatus status = new UserStatus();
	public static ConcurrentHashMap<Integer, Thread> clientThreads = new ConcurrentHashMap<Integer, Thread>();
	public static ConcurrentHashMap<Integer, Socket> clientSocket = new ConcurrentHashMap<Integer, Socket>();
	public static ConcurrentHashMap<Integer, ObjectInputStream> clientIStream = new ConcurrentHashMap<Integer, ObjectInputStream>();
	public static ConcurrentHashMap<Integer, ObjectOutputStream> clientOStream = new ConcurrentHashMap<Integer, ObjectOutputStream>();

	public static void setGlobalFont(String font) {
		// 设置字体名称，样式，大小
		Font fontRes = new Font(font, Font.PLAIN, 12);

		for (Enumeration keys = UIManager.getDefaults().keys();keys.hasMoreElements();) {
			Object key = keys.nextElement();
			Object value = UIManager.get(key);
			if (value instanceof FontUIResource) {
				UIManager.put(key, new FontUIResource(fontRes));
			}
		}
	}
	
	/**
	 * 服务器关闭方法
	 */
	public static void serverClose(){
		// 关闭服务,发包到所有客户端,告知服务器已关闭
		// 找到所有用户端的输出流,发送包到客户端
		for (Integer uid : clientOStream.keySet()) {
			ObjectOutputStream oos = clientOStream.get(uid);
			ServerOffline offlineMsg = new ServerOffline(uid);
			try {
				oos.writeObject(offlineMsg);
				oos.flush();
			} catch (IOException e) {
				// do nothing
			}
		}
	}
}
